﻿namespace ApiTemplate.Common
{
    /// <summary>
    /// 解、压缩相关帮助类
    /// </summary>
    public class ZipHelper
    {
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="filesPath">要压缩的文件夹</param>
        /// <param name="zipFilePath">压缩后的压缩包路径</param>
        public static void CreateZip(string filesPath, string zipFilePath)
        {
            ZipFile.CreateFromDirectory(filesPath, zipFilePath, CompressionLevel.Fastest, false);
        }

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="zipFilePath">压缩包路径</param>
        public static void UnZipFile(string zipFilePath)
        {
            if (!File.Exists(zipFilePath))
            {
                throw new BusinessException()
                {
                    Code = ErrorCode.DataDeleted,
                    ErrorMessage = "文件或文件夹为空"
                };
            }
            FileInfo fileInfo = new(zipFilePath);
            ZipFile.ExtractToDirectory(zipFilePath, fileInfo.Directory.FullName, true);
        }
    }
}
